package com.mmall.other.example.jvm;

import java.util.EmptyStackException;

/**
 * @ClassName OutOfMemoryExample1
 * @Describe :
 * @Author : wangfei
 * @Date :  2019/10/23 16:38
 */
public class OutOfMemoryExample2 {
	private Object[] elements=new Object[10];
	private int size = 0;
	public void push(Object e){
		ensureCapacity();
		elements[size++] = e;
	}
	public Object pop(){
		if( size == 0)
			throw new EmptyStackException();
		return elements[size];
	}
	private void ensureCapacity(){
		if(elements.length == size){
			Object[] oldElements = elements;
			elements = new Object[2 * elements.length+1];
			System.arraycopy(oldElements,0, elements, 0, size);
		}
	}
}
